package com.tianji.promotion.strategy.discount;

import com.tianji.common.utils.NumberUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.promotion.annotation.DiscountTypeFlag;
import com.tianji.promotion.domain.po.Coupon;
import com.tianji.promotion.enums.DiscountType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@DiscountTypeFlag(DiscountType.PER_PRICE_DISCOUNT)
public class PerPriceDiscount implements Discount {

    private final static String RULE_TEMPLATE = "每满{}减{}，上限{}";

    @Override
    public int calculateDiscount(int totalAmount, Coupon coupon) {
        int discount = totalAmount / coupon.getThresholdAmount() * coupon.getDiscountValue();
       /*
        int discount = 0;
        Integer thresholdAmount = coupon.getThresholdAmount();   //100
        Integer discountValue = coupon.getDiscountValue();   //10
        while (totalAmount >= thresholdAmount) {   //303
            discount += discountValue;
            totalAmount -= thresholdAmount;
        }*/
        return Math.min(discount, coupon.getMaxDiscountAmount());
    }

    @Override
    public String getRule(Coupon coupon) {
        return StringUtils.format(
                RULE_TEMPLATE,
                NumberUtils.scaleToStr(coupon.getThresholdAmount(), 2),
                NumberUtils.scaleToStr(coupon.getDiscountValue(), 2),
                NumberUtils.scaleToStr(coupon.getMaxDiscountAmount(), 2));
    }
}
